package cn.itcast.DownloadTest;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DownloadThread extends Thread{
    //定义成员属性
    private String srcFile;
    private String descFile;
    //无参构造方法
    public DownloadThread(){}
    //有参构造方法,用来将两个文件地址作为参数传入
    public DownloadThread(String srcFile,String descFile){
        this.srcFile = srcFile;
        this.descFile = descFile;
    }
    //重写run方法，线程任务内容。
    @Override
    public void run() {
            FileInputStream in = null;
            FileOutputStream out = null;

        try{
            //创建一个字节流写入对象
            in = new FileInputStream(srcFile);
            //创建一个字节流写出对象
             out = new FileOutputStream(descFile);

            //读取该文件的内容
            //定义读取的个数
            int len = -1;
            //利用数组,提高读取效率
            byte[] bytes =new  byte[1024*4];
            //循环读取数据
            while ((len = in.read(bytes))!=-1){
                System.out.println(descFile+"正在下载……");
                //将读取的数据写入对应的目标路径文件
                out.write(bytes,0,len);
            }

        }
       catch (IOException e){
            e.printStackTrace();
       }
       finally {
            //关闭资源
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
